home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / ImageMagick / magick / utility.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  32.4 KB  |  1,094 lines

  1. /*
  2. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3. %                                                                             %
  4. %                                                                             %
  5. %                                                                             %
  6. %             U   U  TTTTT  IIIII  L      IIIII  TTTTT  Y   Y                 %
  7. %             U   U    T      I    L        I      T     Y Y                  %
  8. %             U   U    T      I    L        I      T      Y                   %
  9. %             U   U    T      I    L        I      T      Y                   %
  10. %              UUU     T    IIIII  LLLLL  IIIII    T      Y                   %
  11. %                                                                             %
  12. %                                                                             %
  13. %                       ImageMagick Utility Routines                          %
  14. %                                                                             %
  15. %                                                                             %
  16. %                                                                             %
  17. %                             Software Design                                 %
  18. %                               John Cristy                                   %
  19. %                              January 1993                                   %
  20. %                                                                             %
  21. %                                                                             %
  22. %  Copyright 1994 E. I. du Pont de Nemours & Company                          %
  23. %                                                                             %
  24. %  Permission to use, copy, modify, distribute, and sell this software and    %
  25. %  its documentation for any purpose is hereby granted without fee,           %
  26. %  provided that the above Copyright notice appear in all copies and that     %
  27. %  both that Copyright notice and this permission notice appear in            %
  28. %  supporting documentation, and that the name of E. I. du Pont de Nemours    %
  29. %  & Company not be used in advertising or publicity pertaining to            %
  30. %  distribution of the software without specific, written prior               %
  31. %  permission.  E. I. du Pont de Nemours & Company makes no representations   %
  32. %  about the suitability of this software for any purpose.  It is provided    %
  33. %  "as is" without express or implied warranty.                               %
  34. %                                                                             %
  35. %  E. I. du Pont de Nemours & Company disclaims all warranties with regard    %
  36. %  to this software, including all implied warranties of merchantability      %
  37. %  and fitness, in no event shall E. I. du Pont de Nemours & Company be       %
  38. %  liable for any special, indirect or consequential damages or any           %
  39. %  damages whatsoever resulting from loss of use, data or profits, whether    %
  40. %  in an action of contract, negligence or other tortuous action, arising     %
  41. %  out of or in connection with the use or performance of this software.      %
  42. %                                                                             %
  43. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  44. %
  45. %
  46. */
  47.  
  48. /*
  49.   Include declarations.
  50. */
  51. #include "magick.h"
  52. #include "image.h"
  53. #include "utility.h"
  54. #include "X.h"
  55.  
  56. /*
  57.   Forward declarations.
  58. */
  59. unsigned int
  60.   ReadData _Declare((char *,int,int,FILE *));
  61.  
  62. /*
  63. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  64. %                                                                             %
  65. %                                                                             %
  66. %                                                                             %
  67. %   G l o b E x p e s s i o n                                                 %
  68. %                                                                             %
  69. %                                                                             %
  70. %                                                                             %
  71. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  72. %
  73. %  Function GlobExpression returns True if the expression matches the pattern.
  74. %
  75. %  The format of the GlobExpression function is:
  76. %
  77. %      GlobExpression(expression,pattern)
  78. %
  79. %  A description of each parameter follows:
  80. %
  81. %    o expression: Specifies a pointer to a text string containing a file name.
  82. %
  83. %    o pattern: Specifies a pointer to a text string containing a pattern.
  84. %
  85. %
  86. */
  87. int GlobExpression(expression,pattern)
  88. char
  89.   *expression,
  90.   *pattern;
  91. {
  92.   int
  93.     done;
  94.  
  95.   if (pattern == (char *) NULL)
  96.     return(True);
  97.   if (strlen(pattern) == 0)
  98.     return(True);
  99.   if (strcmp(pattern,"*") == 0)
  100.     return(True);
  101.   done=False;
  102.   while ((*pattern != '\0') && !done)
  103.   {
  104.     if (*expression == '\0')
  105.       if ((*pattern != '{') && (*pattern != '*'))
  106.         break;
  107.     switch (*pattern)
  108.     {
  109.       case '\\':
  110.       {
  111.         pattern++;
  112.         if (*pattern != '\0')
  113.           pattern++;
  114.         break;
  115.       }
  116.       case '*':
  117.       {
  118.         int
  119.           status;
  120.  
  121.         pattern++;
  122.         status=False;
  123.         while ((*expression != '\0') && !status)
  124.           status=GlobExpression(expression++,pattern);
  125.         if (status)
  126.           {
  127.             while (*expression != '\0')
  128.               expression++;
  129.             while (*pattern != '\0')
  130.               pattern++;
  131.           }
  132.         break;
  133.       }
  134.       case '[':
  135.       {
  136.         char
  137.           c;
  138.  
  139.         pattern++;
  140.         for ( ; ; )
  141.         {
  142.           if ((*pattern == '\0') || (*pattern == ']'))
  143.             {
  144.               done=True;
  145.               break;
  146.             }
  147.           if (*pattern == '\\')
  148.             {
  149.               pattern++;
  150.               if (*pattern == '\0')
  151.                 {
  152.                   done=True;
  153.                   break;
  154.                 }
  155.              }
  156.           if (*(pattern+1) == '-')
  157.             {
  158.               c=(*pattern);
  159.               pattern+=2;
  160.               if (*pattern == ']')
  161.                 {
  162.                   done=True;
  163.                   break;
  164.                 }
  165.               if (*pattern == '\\')
  166.                 {
  167.                   pattern++;
  168.                   if (*pattern == '\0')
  169.                     {
  170.                       done=True;
  171.                       break;
  172.                     }
  173.                 }
  174.               if ((*expression < c) || (*expression > *pattern))
  175.                 {
  176.                   pattern++;
  177.                   continue;
  178.                 }
  179.             }
  180.           else
  181.             if (*pattern != *expression)
  182.               {
  183.                 pattern++;
  184.                 continue;
  185.               }
  186.           pattern++;
  187.           while ((*pattern != ']') && (*pattern != '\0'))
  188.           {
  189.             if ((*pattern == '\\') && (*(pattern+1) != '\0'))
  190.               pattern++;
  191.             pattern++;
  192.           }
  193.           if (*pattern != '\0')
  194.             {
  195.               pattern++;
  196.               expression++;
  197.             }
  198.           break;
  199.         }
  200.         break;
  201.       }
  202.       case '?':
  203.       {
  204.         pattern++;
  205.         expression++;
  206.         break;
  207.       }
  208.       case '{':
  209.       {
  210.         int
  211.           match;
  212.  
  213.         register char
  214.           *p;
  215.  
  216.         pattern++;
  217.         while ((*pattern != '}') && (*pattern != '\0'))
  218.         {
  219.           p=expression;
  220.           match=True;
  221.           while ((*p != '\0') && (*pattern != '\0') &&
  222.                  (*pattern != ',') && (*pattern != '}') && match)
  223.           {
  224.             if (*pattern == '\\')
  225.               pattern++;
  226.             match=(*pattern == *p);
  227.             p++;
  228.             pattern++;
  229.           }
  230.           if (*pattern == '\0')
  231.             {
  232.               match=False;
  233.               done=True;
  234.               break;
  235.             }
  236.           else
  237.             if (match)
  238.               {
  239.                 expression=p;
  240.                 while ((*pattern != '}') && (*pattern != '\0'))
  241.                 {
  242.                   pattern++;
  243.                   if (*pattern == '\\')
  244.                     {
  245.                       pattern++;
  246.                       if (*pattern == '}')
  247.                         pattern++;
  248.                     }
  249.                 }
  250.               }
  251.             else
  252.               {
  253.                 while ((*pattern != '}') && (*pattern != ',') &&
  254.                        (*pattern != '\0'))
  255.                 {
  256.                   pattern++;
  257.                   if (*pattern == '\\')
  258.                     {
  259.                       pattern++;
  260.                       if ((*pattern == '}') || (*pattern == ','))
  261.                         pattern++;
  262.                     }
  263.                 }
  264.               }
  265.             if (*pattern != '\0')
  266.               pattern++;
  267.           }
  268.         break;
  269.       }
  270.       default:
  271.       {
  272.         if (*expression != *pattern)
  273.           done=True;
  274.         else
  275.           {
  276.             expression++;
  277.             pattern++;
  278.           }
  279.       }
  280.     }
  281.   }
  282.   while (*pattern == '*')
  283.     pattern++;
  284.   return((*expression == '\0') && (*pattern == '\0'));
  285. }
  286.  
  287. /*
  288. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  289. %                                                                             %
  290. %                                                                             %
  291. %                                                                             %
  292. %   L i s t F i l e s                                                         %
  293. %                                                                             %
  294. %                                                                             %
  295. %                                                                             %
  296. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  297. %
  298. %  Function ListFiles reads the directory specified and returns a list
  299. %  of filenames contained in the directory sorted in ascending alphabetic
  300. %  order.
  301. %
  302. %  The format of the ListFiles function is:
  303. %
  304. %      filelist=ListFiles(directory,pattern,number_entries)
  305. %
  306. %  A description of each parameter follows:
  307. %
  308. %    o filelist: Function ListFiles returns a list of filenames contained
  309. %      in the directory.  If the directory specified cannot be read or it is
  310. %      a file a NULL list is returned.
  311. %
  312. %    o directory: Specifies a pointer to a text string containing a directory
  313. %      name.
  314. %
  315. %    o pattern: Specifies a pointer to a text string containing a pattern.
  316. %
  317. %    o number_entries:  This integer returns the number of filenames in the
  318. %      list.
  319. %
  320. %
  321. */
  322. static int FileCompare(x,y)
  323. const void
  324.   *x,
  325.   *y;
  326. {
  327.   register char
  328.     *p,
  329.     *q;
  330.  
  331.   p=(char *) *((char **) x);
  332.   q=(char *) *((char **) y);
  333.   while ((*p != '\0') && (*q != '\0') && (*p == *q))
  334.   {
  335.     p++;
  336.     q++;
  337.   }
  338.   return(*p-(*q));
  339. }
  340.  
  341. char **ListFiles(directory,pattern,number_entries)
  342. char
  343.   *directory,
  344.   *pattern;
  345.  
  346. int
  347.   *number_entries;
  348. {
  349. #ifndef vms
  350. #define UpDirectoryName  ".."
  351.  
  352.   char
  353.     **filelist,
  354.     working_directory[MaxTextLength];
  355.  
  356.   DIR
  357.     *current_directory;
  358.  
  359.   struct dirent
  360.     *entry;
  361.  
  362.   struct stat
  363.     file_status;
  364.  
  365.   unsigned int
  366.     max_entries;
  367.  
  368.   /*
  369.     Open directory.
  370.   */
  371.   *number_entries=0;
  372.   current_directory=opendir(directory);
  373.   if (current_directory == (DIR *) NULL)
  374.     return((char **) NULL);
  375.   /*
  376.     Allocate filelist.
  377.   */
  378.   max_entries=2048;
  379.   filelist=(char **) malloc(max_entries*sizeof(char *));
  380.   if (filelist == (char **) NULL)
  381.     {
  382.       (void) closedir(current_directory);
  383.       return((char **) NULL);
  384.     }
  385.   /*
  386.     Save the current and change to the new directory.
  387.   */
  388.   (void) getcwd(working_directory,MaxTextLength-1);
  389.   (void) chdir(directory);
  390.   entry=readdir(current_directory);
  391.   while (entry != (struct dirent *) NULL)
  392.   {
  393.     if (*entry->d_name == '.')
  394.       {
  395.         entry=readdir(current_directory);
  396.         continue;
  397.       }
  398.     (void) stat(entry->d_name,&file_status);
  399.     if (S_ISDIR(file_status.st_mode) || GlobExpression(entry->d_name,pattern))
  400.       {
  401.         if (*number_entries >= max_entries)
  402.           {
  403.             max_entries<<=1;
  404.             filelist=(char **)
  405.               realloc((char *) filelist,max_entries*sizeof(char *));
  406.             if (filelist == (char **) NULL)
  407.               {
  408.                 (void) closedir(current_directory);
  409.                 return((char **) NULL);
  410.               }
  411.           }
  412.         filelist[*number_entries]=(char *) malloc(strlen(entry->d_name)+2);
  413.         if (filelist[*number_entries] == (char *) NULL)
  414.           break;
  415.         (void) strcpy(filelist[*number_entries],entry->d_name);
  416.         if (S_ISDIR(file_status.st_mode))
  417.           (void) strcat(filelist[*number_entries],DirectorySeparator);
  418.         (*number_entries)++;
  419.       }
  420.     entry=readdir(current_directory);
  421.   }
  422.   (void) closedir(current_directory);
  423.   /*
  424.     Sort filelist in ascending order.
  425.   */
  426.   (void) qsort((void *) filelist,*number_entries,sizeof(char **),FileCompare);
  427.   /*
  428.     Determine current working directory.
  429.   */
  430.   (void) chdir(working_directory);
  431.   (void) chdir(directory);
  432.   (void) getcwd(directory,MaxTextLength-1);
  433.   return(filelist);
  434. #else
  435.   return((char **) NULL);
  436. #endif
  437. }
  438.  
  439. /*
  440. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  441. %                                                                             %
  442. %                                                                             %
  443. %                                                                             %
  444. %  L S B F i r s t R e a d L o n g                                            %
  445. %                                                                             %
  446. %                                                                             %
  447. %                                                                             %
  448. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  449. %
  450. %  Function LSBFirstReadLong reads a long value as a 32 bit quantity in
  451. %  least-significant byte first order.
  452. %
  453. %  The format of the LSBFirstReadLong routine is:
  454. %
  455. %       value=LSBFirstReadLong(file)
  456. %
  457. %  A description of each parameter follows.
  458. %
  459. %    o value:  Function LSBFirstReadLong returns an unsigned long read from
  460. %      the file.
  461. %
  462. %   o  file:  Specifies the file to read the data from.
  463. %
  464. %
  465. */
  466. unsigned long LSBFirstReadLong(file)
  467. FILE
  468.   *file;
  469. {
  470.   unsigned char
  471.     buffer[4];
  472.  
  473.   unsigned int
  474.     status;
  475.  
  476.   unsigned long
  477.     value;
  478.  
  479.   status=ReadData((char *) buffer,1,4,file);
  480.   if (status == False)
  481.     return((unsigned long) ~0);
  482.   value=(unsigned int) (buffer[3] << 24);
  483.   value|=(unsigned int) (buffer[2] << 16);
  484.   value|=(unsigned int) (buffer[1] << 8);
  485.   value|=(unsigned int) (buffer[0]);
  486.   return(value);
  487. }
  488.  
  489. /*
  490. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  491. %                                                                             %
  492. %                                                                             %
  493. %                                                                             %
  494. %  L S B F i r s t R e a d S h o r t                                          %
  495. %                                                                             %
  496. %                                                                             %
  497. %                                                                             %
  498. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  499. %
  500. %  Function LSBFirstReadShort reads a short value as a 16 bit quantity in
  501. %  least-significant byte first order.
  502. %
  503. %  The format of the LSBFirstReadShort routine is:
  504. %
  505. %       value=LSBFirstReadShort(file)
  506. %
  507. %  A description of each parameter follows.
  508. %
  509. %    o value:  Function LSBFirstReadShort returns an unsigned short read from
  510. %      the file.
  511. %
  512. %   o  file:  Specifies the file to read the data from.
  513. %
  514. %
  515. */
  516. unsigned short LSBFirstReadShort(file)
  517. FILE
  518.   *file;
  519. {
  520.   unsigned char
  521.     buffer[2];
  522.  
  523.   unsigned int
  524.     status;
  525.  
  526.   unsigned short
  527.     value;
  528.  
  529.   status=ReadData((char *) buffer,1,2,file);
  530.   if (status == False)
  531.     return((unsigned short) ~0);
  532.   value=(unsigned short) (buffer[1] << 8);
  533.   value|=(unsigned short) (buffer[0]);
  534.   return(value);
  535. }
  536.  
  537. /*
  538. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  539. %                                                                             %
  540. %                                                                             %
  541. %                                                                             %
  542. %  L S B F i r s t W r i t e L o n g                                          %
  543. %                                                                             %
  544. %                                                                             %
  545. %                                                                             %
  546. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  547. %
  548. %  Function LSBFirstWriteLong writes a long value as a 32 bit quantity in
  549. %  least-significant byte first order.
  550. %
  551. %  The format of the LSBFirstWriteLong routine is:
  552. %
  553. %       LSBFirstWriteLong(value,file)
  554. %
  555. %  A description of each parameter follows.
  556. %
  557. %   o  value:  Specifies the value to write.
  558. %
  559. %   o  file:  Specifies the file to write the data to.
  560. %
  561. %
  562. */
  563. void LSBFirstWriteLong(value,file)
  564. unsigned long
  565.   value;
  566.  
  567. FILE
  568.   *file;
  569. {
  570.   unsigned char
  571.     buffer[4];
  572.  
  573.   buffer[0]=(unsigned char) (value);
  574.   buffer[1]=(unsigned char) ((value) >> 8);
  575.   buffer[2]=(unsigned char) ((value) >> 16);
  576.   buffer[3]=(unsigned char) ((value) >> 24);
  577.   (void) fwrite((char *) buffer,1,4,file);
  578. }
  579.  
  580. /*
  581. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  582. %                                                                             %
  583. %                                                                             %
  584. %                                                                             %
  585. %  L S B F i r s t W r i t e S h o r t                                        %
  586. %                                                                             %
  587. %                                                                             %
  588. %                                                                             %
  589. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  590. %
  591. %  Function LSBFirstWriteShort writes a long value as a 16 bit quantity in
  592. %  least-significant byte first order.
  593. %
  594. %  The format of the LSBFirstWriteShort routine is:
  595. %
  596. %       LSBFirstWriteShort(value,file)
  597. %
  598. %  A description of each parameter follows.
  599. %
  600. %   o  value:  Specifies the value to write.
  601. %
  602. %   o  file:  Specifies the file to write the data to.
  603. %
  604. %
  605. */
  606. void LSBFirstWriteShort(value,file)
  607. unsigned int
  608.   value;
  609.  
  610. FILE
  611.   *file;
  612. {
  613.   unsigned char
  614.     buffer[2];
  615.  
  616.   buffer[0]=(unsigned char) (value);
  617.   buffer[1]=(unsigned char) ((value) >> 8);
  618.   (void) fwrite((char *) buffer,1,2,file);
  619. }
  620.  
  621. /*
  622. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  623. %                                                                             %
  624. %                                                                             %
  625. %                                                                             %
  626. %  M S B F i r s t O r d e r L o n g                                          %
  627. %                                                                             %
  628. %                                                                             %
  629. %                                                                             %
  630. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  631. %
  632. %  Function MSBFirstOrderLong converts a least-significant byte first buffer
  633. %  of integers to most-significant byte first.
  634. %
  635. %  The format of the MSBFirstOrderLong routine is:
  636. %
  637. %       MSBFirstOrderLong(p,length);
  638. %
  639. %  A description of each parameter follows.
  640. %
  641. %   o  p:  Specifies a pointer to a buffer of integers.
  642. %
  643. %   o  length:  Specifies the length of the buffer.
  644. %
  645. %
  646. */
  647. void MSBFirstOrderLong(p,length)
  648. register char
  649.   *p;
  650.  
  651. register unsigned int
  652.   length;
  653. {
  654.   register char
  655.     c,
  656.     *q,
  657.     *sp;
  658.  
  659.   q=p+length;
  660.   while (p < q)
  661.   {
  662.     sp=p+3;
  663.     c=(*sp);
  664.     *sp=(*p);
  665.     *p++=c;
  666.     sp=p+1;
  667.     c=(*sp);
  668.     *sp=(*p);
  669.     *p++=c;
  670.     p+=2;
  671.   }
  672. }
  673.  
  674. /*
  675. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  676. %                                                                             %
  677. %                                                                             %
  678. %                                                                             %
  679. %  M S B F i r s t O r d e r S h o r t                                        %
  680. %                                                                             %
  681. %                                                                             %
  682. %                                                                             %
  683. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  684. %
  685. %  Function MSBFirstOrderShort converts a least-significant byte first buffer
  686. %  of integers to most-significant byte first.
  687. %
  688. %  The format of the MSBFirstOrderShort routine is:
  689. %
  690. %       MSBFirstOrderLongShort(p,length);
  691. %
  692. %  A description of each parameter follows.
  693. %
  694. %   o  p:  Specifies a pointer to a buffer of integers.
  695. %
  696. %   o  length:  Specifies the length of the buffer.
  697. %
  698. %
  699. */
  700. void MSBFirstOrderShort(p,length)
  701. register char
  702.   *p;
  703.  
  704. register unsigned int
  705.   length;
  706. {
  707.   register char
  708.     c,
  709.     *q;
  710.  
  711.   q=p+length;
  712.   while (p < q)
  713.   {
  714.     c=(*p);
  715.     *p=(*(p+1));
  716.     p++;
  717.     *p++=c;
  718.   }
  719. }
  720.  
  721. /*
  722. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  723. %                                                                             %
  724. %                                                                             %
  725. %                                                                             %
  726. %  M S B F i r s t R e a d S h o r t                                          %
  727. %                                                                             %
  728. %                                                                             %
  729. %                                                                             %
  730. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  731. %
  732. %  Function MSBFirstReadShort reads a short value as a 16 bit quantity in
  733. %  most-significant byte first order.
  734. %
  735. %  The format of the MSBFirstReadShort routine is:
  736. %
  737. %       value=MSBFirstReadShort(file)
  738. %
  739. %  A description of each parameter follows.
  740. %
  741. %    o value:  Function MSBFirstReadShort returns an unsigned short read from
  742. %      the file.
  743. %
  744. %   o  file:  Specifies the file to read the data from.
  745. %
  746. %
  747. */
  748. unsigned short MSBFirstReadShort(file)
  749. FILE
  750.   *file;
  751. {
  752.   unsigned char
  753.     buffer[2];
  754.  
  755.   unsigned int
  756.     status;
  757.  
  758.   unsigned short
  759.     value;
  760.  
  761.   status=ReadData((char *) buffer,1,2,file);
  762.   if (status == False)
  763.     return((unsigned short) ~0);
  764.   value=(unsigned int) (buffer[0] << 8);
  765.   value|=(unsigned int) (buffer[1]);
  766.   return(value);
  767. }
  768.  
  769. /*
  770. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  771. %                                                                             %
  772. %                                                                             %
  773. %                                                                             %
  774. %  M S B F i r s t R e a d L o n g                                            %
  775. %                                                                             %
  776. %                                                                             %
  777. %                                                                             %
  778. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  779. %
  780. %  Function MSBFirstReadLong reads a long value as a 32 bit quantity in
  781. %  most-significant byte first order.
  782. %
  783. %  The format of the MSBFirstReadLong routine is:
  784. %
  785. %       value=MSBFirstReadLong(file)
  786. %
  787. %  A description of each parameter follows.
  788. %
  789. %    o value:  Function MSBFirstReadLong returns an unsigned long read from
  790. %      the file.
  791. %
  792. %   o  file:  Specifies the file to read the data from.
  793. %
  794. %
  795. */
  796. unsigned long MSBFirstReadLong(file)
  797. FILE
  798.   *file;
  799. {
  800.   unsigned char
  801.     buffer[4];
  802.  
  803.   unsigned int
  804.     status;
  805.  
  806.   unsigned long
  807.     value;
  808.  
  809.   status=ReadData((char *) buffer,1,4,file);
  810.   if (status == False)
  811.     return((unsigned long) ~0);
  812.   value=(unsigned int) (buffer[0] << 24);
  813.   value|=(unsigned int) (buffer[1] << 16);
  814.   value|=(unsigned int) (buffer[2] << 8);
  815.   value|=(unsigned int) (buffer[3]);
  816.   return(value);
  817. }
  818.  
  819. /*
  820. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  821. %                                                                             %
  822. %                                                                             %
  823. %                                                                             %
  824. %  M S B F i r s t W r i t e L o n g                                          %
  825. %                                                                             %
  826. %                                                                             %
  827. %                                                                             %
  828. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  829. %
  830. %  Function MSBFirstWriteLong writes a long value as a 32 bit quantity in
  831. %  most-significant byte first order.
  832. %
  833. %  The format of the MSBFirstWriteLong routine is:
  834. %
  835. %       MSBFirstWriteLong(value,file)
  836. %
  837. %  A description of each parameter follows.
  838. %
  839. %   o  value:  Specifies the value to write.
  840. %
  841. %   o  file:  Specifies the file to write the data to.
  842. %
  843. %
  844. */
  845. void MSBFirstWriteLong(value,file)
  846. unsigned long
  847.   value;
  848.  
  849. FILE
  850.   *file;
  851. {
  852.   unsigned char
  853.     buffer[4];
  854.  
  855.   buffer[0]=(unsigned char) ((value) >> 24);
  856.   buffer[1]=(unsigned char) ((value) >> 16);
  857.   buffer[2]=(unsigned char) ((value) >> 8);
  858.   buffer[3]=(unsigned char) (value);
  859.   (void) fwrite((char *) buffer,1,4,file);
  860. }
  861.  
  862. /*
  863. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  864. %                                                                             %
  865. %                                                                             %
  866. %                                                                             %
  867. %  M S B F i r s t W r i t e S h o r t                                        %
  868. %                                                                             %
  869. %                                                                             %
  870. %                                                                             %
  871. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  872. %
  873. %  Function MSBFirstWriteShort writes a long value as a 16 bit quantity in
  874. %  most-significant byte first order.
  875. %
  876. %  The format of the MSBFirstWriteShort routine is:
  877. %
  878. %       MSBFirstWriteShort(value,file)
  879. %
  880. %  A description of each parameter follows.
  881. %
  882. %   o  value:  Specifies the value to write.
  883. %
  884. %   o  file:  Specifies the file to write the data to.
  885. %
  886. %
  887. */
  888. void MSBFirstWriteShort(value,file)
  889. unsigned int
  890.   value;
  891.  
  892. FILE
  893.   *file;
  894. {
  895.   unsigned char
  896.     buffer[2];
  897.  
  898.   buffer[0]=(unsigned char) ((value) >> 8);
  899.   buffer[1]=(unsigned char) (value);
  900.   (void) fwrite((char *) buffer,1,2,file);
  901. }
  902.  
  903. /*
  904. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  905. %                                                                             %
  906. %                                                                             %
  907. %                                                                             %
  908. %  R e a d D a t a                                                            %
  909. %                                                                             %
  910. %                                                                             %
  911. %                                                                             %
  912. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  913. %
  914. %  Function ReadData reads data from the image file and returns it.  If it
  915. %  cannot read the requested number of items, False is returned indicating
  916. %  an error.
  917. %
  918. %  The format of the ReadData routine is:
  919. %
  920. %      status=ReadData(data,size,number_items,file)
  921. %
  922. %  A description of each parameter follows:
  923. %
  924. %    o status:  Function ReadData returns True if all the data requested
  925. %      is obtained without error, otherwise False.
  926. %
  927. %    o data:  Specifies an area to place the information reuested from
  928. %      the file.
  929. %
  930. %    o size:  Specifies an integer representing the length of an
  931. %      individual item to be read from the file.
  932. %
  933. %    o number_items:  Specifies an integer representing the number of items
  934. %      to read from the file.
  935. %
  936. %    o file:  Specifies a file to read the data.
  937. %
  938. %
  939. */
  940. unsigned int ReadData(data,size,number_items,file)
  941. char
  942.   *data;
  943.  
  944. int
  945.   size,
  946.   number_items;
  947.  
  948. FILE
  949.   *file;
  950. {
  951.   size*=number_items;
  952.   while (size > 0)
  953.   {
  954.     number_items=fread(data,1,size,file);
  955.     if (number_items <= 0)
  956.       return(False);
  957.     size-=number_items;
  958.     data+=number_items;
  959.   }
  960.   return(True);
  961. }
  962.  
  963. /*
  964. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  965. %                                                                             %
  966. %                                                                             %
  967. %                                                                             %
  968. %  R e a d D a t a B l o c k                                                  %
  969. %                                                                             %
  970. %                                                                             %
  971. %                                                                             %
  972. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  973. %
  974. %  Function ReadDataBlock reads data from the image file and returns it.  The
  975. %  amount of data is determined by first reading a count byte.  If
  976. %  ReadDataBlock cannot read the requested number of items, `-1' is returned
  977. %  indicating an error.
  978. %
  979. %  The format of the ReadData routine is:
  980. %
  981. %      status=ReadData(data,file)
  982. %
  983. %  A description of each parameter follows:
  984. %
  985. %    o status:  Function ReadData returns the number of characters read
  986. %      unless there is an error, otherwise `-1'.
  987. %
  988. %    o data:  Specifies an area to place the information reuested from
  989. %      the file.
  990. %
  991. %    o file:  Specifies a file to read the data.
  992. %
  993. %
  994. */
  995. int ReadDataBlock(data,file)
  996. char
  997.   *data;
  998.  
  999. FILE
  1000.   *file;
  1001. {
  1002.   unsigned char
  1003.     count;
  1004.  
  1005.   unsigned int
  1006.     status;
  1007.  
  1008.   status=ReadData((char *) &count,1,1,file);
  1009.   if (status == False)
  1010.     return(-1);
  1011.   if (count == 0)
  1012.     return(0);
  1013.   status=ReadData(data,1,(int) count,file);
  1014.   if (status == False)
  1015.     return(-1);
  1016.   return(count);
  1017. }
  1018.  
  1019. /*
  1020. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1021. %                                                                             %
  1022. %                                                                             %
  1023. %                                                                             %
  1024. %  S t r i n g T o L i s t                                                    %
  1025. %                                                                             %
  1026. %                                                                             %
  1027. %                                                                             %
  1028. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1029. %
  1030. %  Function StringToList converts a text string into a list by segmenting the
  1031. %  text string at each carriage return discovered.
  1032. %
  1033. %  The format of the StringToList routine is:
  1034. %
  1035. %      list=StringToList(text)
  1036. %
  1037. %  A description of each parameter follows:
  1038. %
  1039. %    o list:  Function StringToList returns the string list unless an error
  1040. %      occurs, otherwise NULL.
  1041. %
  1042. %    o text:  Specifies the string to segment into a list.
  1043. %
  1044. %
  1045. */
  1046. char **StringToList(text)
  1047. char
  1048.   *text;
  1049. {
  1050.   char
  1051.     **textlist;
  1052.  
  1053.   register char
  1054.     *p,
  1055.     *q;
  1056.  
  1057.   register int
  1058.     i;
  1059.  
  1060.   unsigned int
  1061.     lines;
  1062.  
  1063.   if (text == (char *) NULL)
  1064.     return((char **) NULL);
  1065.   lines=1;
  1066.   for (p=text; *p != '\0'; p++)
  1067.     if (*p == '\n')
  1068.       lines++;
  1069.   textlist=(char **) malloc((lines+1)*sizeof(char *));
  1070.   if (textlist == (char **) NULL)
  1071.     {
  1072.       Warning("Unable to convert text","Memory allocation failed");
  1073.       return((char **) NULL);
  1074.     }
  1075.   p=text;
  1076.   for (i=0; i < lines; i++)
  1077.   {
  1078.     for (q=p; *q != '\0'; q++)
  1079.       if (*q == '\n')
  1080.         break;
  1081.     textlist[i]=(char *) malloc((q-p+1)*sizeof(char));
  1082.     if (textlist[i] == (char *) NULL)
  1083.       {
  1084.         Warning("Unable to convert text","Memory allocation failed");
  1085.         return((char **) NULL);
  1086.       }
  1087.     (void) strncpy(textlist[i],p,q-p);
  1088.     textlist[i][q-p]='\0';
  1089.     p=q+1;
  1090.   }
  1091.   textlist[i]=(char *) NULL;
  1092.   return(textlist);
  1093. }
  1094.